package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {
    @Value("$(sms.url)")
    private String smsUrl;
    @Value("$(sms.appkey)")
    private String appkey;


    @Autowired
    private MongoTemplate template;
    @Autowired
    private DestinationRepository repository;
    @Autowired
    private RegionServiceImpl regionService;

    @Override
    public void save(Destination destination) {
        destination.setId(null);
        repository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        repository.save(destination);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        Destination destination = repository.findById(id).orElse(null);
        return destination;
    }

    @Override
    public List<Destination> list() {

        return repository.findAll();
    }

    /**
     * 通过id查对象
     *
     * @param rid
     * @return
     */
    @Override
    public List<Destination> queryDtByionId(String rid) {
        //通过区域id查询区域对象
        Region region = regionService.get(rid);
        //获取对象下的目的id集合
        List<String> refIds = region.getRefIds();
        //通过id集合查询目的地集合
        return repository.findByIdIn(refIds);
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {
        //分页查询
        //查询对象
        //1: 查询总数:total
        Query query = new Query();
        //条件
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        if (StringUtils.hasLength(qo.getParentId())) {
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        } /*else {
            //为空
            query.addCriteria(Criteria.where("parentId").is(null));
        }*/

        long total = template.count(query, Destination.class);
        if (total == 0) {
            return Page.empty();  //空的分页对象
        }
        //2: 每页显示条数集合: list
        //设置页面显示条数, 还有当前页
        //1 当前页 2 每页的条数 3 排序规则 4 排序字段
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");
        query.with(pageable);
        List<Destination> list = template.find(query, Destination.class);

        //3:封装page对象
        return new PageImpl<>(list, pageable, total);
    }

    @Override
    public void updateInfo(Destination destination) {
        String id = destination.getId();
        this.update(destination);
    }

    @Override
    public List<Destination> queryDestByRegionIdForApi(String regionId) {
        List<Destination> list = new ArrayList<>();
        //区域:国内的
        if("-1".equals(regionId)){
            //查 下面所有省份
            list = repository.findByParentName("中国");
        }else {
            //国外
            list = this.queryDestByRegionId(regionId);
        }
        //第二层
        for(Destination dest:list){
            //这是所有
            //List<Destination> children =  repository.findByParentId(dest.getId(), pageRequest);
            //前五个子目的地的
           PageRequest pageRequest =  PageRequest.of(0,6,Sort.Direction.ASC,"_id");
            List<Destination> children = repository.findByParentId(dest.getId(), pageRequest);
            dest.setChildren(children);
        }
        return list;
    }

    @Override
    public List<Destination> queryDestByRegionId(String regionId) {
       //通过区域id查询区域对象
        Region region = regionService.get(regionId);
        //获取对象挂载目的地id集合
        List<String> ids = region.getRefIds();
        return repository.findByIdIn(ids);
    }
    private void creatToasts(List<Destination> list, String parentId) {

        if (!StringUtils.hasLength(parentId)){
            return;
        }
        Destination byId = this.get(parentId);
        System.err.println(byId);
        list.add(byId);
        if (byId.getParentId()!=null){

            creatToasts(list,byId.getParentId());
        }

    }
    @Override
    public List<Destination> findtoasts(String parentId) {

        List<Destination> list=new ArrayList<>();
        this.creatToasts(list,parentId);


        Collections.reverse(list);
        return list;
    }

    @Override
    public Destination findByName(String keyword) {
        Destination name = repository.findByName(keyword);
        return name;
    }


}

    /*@Override
    public void updateInfo(Destination destination) {
        this.update(destination);
        }
    }*/

//    @Override
//    public void saveOrUpdate(Destination destination) {
//        if(!(StringUtils.hasLength(destination.getId()))){
//            destination.setId(null);
//            this.save(destination);
//        }else {
//            this.update(destination);
//        }
//    }

